package com.ct.application.shipping.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shipping.model.po.ChuanYuanKuGongZuoSavePO;
import com.ct.application.shipping.model.po.ChuanYuanKuGongZuoSearchPO;
import com.ct.application.shipping.model.vo.ChuanYuanKuGongZuoVO;
import com.ct.application.shipping.service.ChuanYuanKuGongZuoService;
import com.ct.repository.commons.BaseServiceImpl;
import com.ct.shipping.mapper.ChuanYuanKuGongZuoMapper;
import com.ct.shipping.orm.ChuanYuanKuFuWuBuDO;
import com.ct.shipping.orm.ChuanYuanKuGongZuoDO;
import com.ct.shipping.orm.ChuanYuanKuGongZuoDO;
import com.ct.utils.date.DateUtils;
import com.ct.utils.threadlocal.UserThreadLocal;
import com.ct.utils.valid.ValidatorUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 船员库工作经验 服务实现类
 * </p>
 *
 * @author carlo
 */
@Service
public class ChuanYuanKuGongZuoServiceImpl extends BaseServiceImpl<ChuanYuanKuGongZuoMapper, ChuanYuanKuGongZuoDO>
        implements ChuanYuanKuGongZuoService {

    /**
     * 查询分页列表
     */
    @Override
    public Page<ChuanYuanKuGongZuoVO> findChuanYuanKuGongZuo(ChuanYuanKuGongZuoSearchPO searchPO) {
        LambdaQueryWrapper<ChuanYuanKuGongZuoDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Objects.nonNull(searchPO.getChuanYuanId()), ChuanYuanKuGongZuoDO::getChuanYuanId,searchPO.getChuanYuanId());
        lambdaQueryWrapper.orderByDesc(ChuanYuanKuGongZuoDO::getRenZhiRiQiSort);
        Page<ChuanYuanKuGongZuoDO> page = new Page<>(searchPO.getPageNo(), searchPO.getPageSize());
        Page<ChuanYuanKuGongZuoDO> page1 = this.page(page, lambdaQueryWrapper);


        return tranferPageBo(page1, ChuanYuanKuGongZuoVO.class);
    }

    public ChuanYuanKuGongZuoDO findLastChuanYuanKuGongZuo() {
        ChuanYuanKuGongZuoDO kuGongZuoDO = this.lambdaQuery()
                .orderByDesc(ChuanYuanKuGongZuoDO::getCreateTime)
                .last("  limit 1 ")
                .one();
        return kuGongZuoDO;
    }


    /**
     * 保存
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ChuanYuanKuGongZuoVO saveChuanYuanKuGongZuo(ChuanYuanKuGongZuoSavePO savePO){
        savePO.setId(null); //新增不需要id
        ChuanYuanKuGongZuoDO chuanYuanKuGongZuoDO = tranferBo(savePO, ChuanYuanKuGongZuoDO.class);

        if (Objects.nonNull(chuanYuanKuGongZuoDO)){
            String renZhiRiQi = savePO.getRenZhiRiQi();
            if (StringUtils.isNotEmpty(renZhiRiQi)) {
                chuanYuanKuGongZuoDO.setRenZhiRiQiSort(DateUtils.stringToDate(renZhiRiQi).getTime());
            }else{
                chuanYuanKuGongZuoDO.setRenZhiRiQiSort(0L);
            }
            chuanYuanKuGongZuoDO.setSystemId(UserThreadLocal.get().getSystemId());
            chuanYuanKuGongZuoDO.setSystemName(UserThreadLocal.get().getSystemName());
            this.save(chuanYuanKuGongZuoDO);
            return tranferBo(chuanYuanKuGongZuoDO, ChuanYuanKuGongZuoVO.class);
        }
        return null;
    }

    /**
     * 修改
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ChuanYuanKuGongZuoVO updateChuanYuanKuGongZuo(ChuanYuanKuGongZuoSavePO savePO){
        if (savePO.getId() == null) {
            throw new RuntimeException("id不能为空");
        }
        ChuanYuanKuGongZuoDO chuanYuanKuGongZuoDO = tranferBo(savePO, ChuanYuanKuGongZuoDO.class);
        String renZhiRiQi = savePO.getRenZhiRiQi();
        if (StringUtils.isNotEmpty(renZhiRiQi)) {
            chuanYuanKuGongZuoDO.setRenZhiRiQiSort(DateUtils.stringToDate(renZhiRiQi).getTime());
        }else{
            chuanYuanKuGongZuoDO.setRenZhiRiQiSort(0L);
        }

        if (Objects.nonNull(chuanYuanKuGongZuoDO)){
            this.updateById(chuanYuanKuGongZuoDO);
            return tranferBo(chuanYuanKuGongZuoDO, ChuanYuanKuGongZuoVO.class);
        }
        return null;
    }


    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean delChuanYuanKuGongZuo(Long id){
        return this.removeById(id);
    }


    /**
     * 职位状态修改。
     * 0:停用、1:启用
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return this.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return this.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }


    @Override
    public ChuanYuanKuGongZuoVO getChuanYuanKuGongZuo(Long id){
        ChuanYuanKuGongZuoDO chuanYuanKuGongZuoDO = getById(id);
        return tranferBo(chuanYuanKuGongZuoDO,ChuanYuanKuGongZuoVO.class);
    }
}
